package com.mcxx.modules.system.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.statistics.entity.StatisticsExportEntity;
import com.mcxx.modules.system.dao.write.StatisticsExportWriteDao;
import com.mcxx.modules.system.dto.StatisticsExportDTO;
import com.mcxx.modules.system.service.read.StatisticsExportReadService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@TaTransactional
public class StatisticsExportWriteService {

    @Autowired
    private StatisticsExportWriteDao statisticsExportWriteDao;
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    private StatisticsExportReadService statisticsExportReadService;


    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void insertIgnoreNull(StatisticsExportEntity statisticsExport){
        int rst = statisticsExportWriteDao.insertIgnoreNull(statisticsExport);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }



    public void updateStatus(StatisticsExportDTO statisticsExportDTO){
        statisticsExportWriteDao.updateStatus(statisticsExportDTO);
    }


    public void updateStatusById(String id, String  status){
        int rst = statisticsExportWriteDao.updateStatusById(id, status);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void updateStatusByIds(List<String> idList, String  status){
        int rst = statisticsExportWriteDao.updateStatusByIds(idList, status);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }



    public void updateIgnoreNull(StatisticsExportEntity statisticsExport){
        int rst = statisticsExportWriteDao.updateIgnoreNull(statisticsExport);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_9999);
        }
    }

    public String addStatistics(String areaCode,String objectType,String years){
        String businessType = "";
        String standType = null;
        if(Constant.GuicaiType.CITY_DIBAO.equals(objectType)){

            businessType = Constant.BusinessCode.DIBAO;
            standType = Constant.BusinessStandType.CITY_DIBAO;
        }else if(Constant.GuicaiType.COUNTY_DIBAO.equals(objectType)){

            businessType = Constant.BusinessCode.DIBAO;
            standType = Constant.BusinessStandType.COUNTY_DIBAO;
        }else if(Constant.GuicaiType.CITY_TEKUN.equals(objectType)){

            businessType = Constant.BusinessCode.TEKUN;
            standType = Constant.BusinessStandType.CITY_TEKUN;
        }else if(Constant.GuicaiType.COUNTY_TEKUN.equals(objectType)){

            businessType = Constant.BusinessCode.TEKUN;
            standType = Constant.BusinessStandType.COUNTY_TEKUN;
        }else{

            businessType = Constant.BusinessCode.LINSHI;
        }

        fundPayReadService.verifyConfirmFundPayThrow(businessType, areaCode,
                standType, years,null,
                Constant.FundType.CAPITAL_FUND_PAY,Constant.FundPayState.CONFIRM);

        String statisId = "";
        StatisticsExportEntity entity =  statisticsExportReadService.selectRepeatByAreaCode(areaCode,objectType,years);
        if(CommonUtil.invalidArgs(entity)){

            StatisticsExportEntity statisticsExportEntity = new StatisticsExportEntity();
            statisticsExportEntity.setId(Constant.getUUID());
            statisticsExportEntity.setAreaCode(areaCode);
            statisticsExportEntity.setObjectType(objectType);
            statisticsExportEntity.setStatus(Constant.StatisticsStatus.CREAT_EXPORT_ING);
            statisticsExportEntity.setYears(years);
            insertIgnoreNull(statisticsExportEntity);
            statisId = statisticsExportEntity.getId();
        }else{
            if(Constant.StatisticsStatus.CREAT_EXPORT_ING.equals(entity.getStatus())){
                throw new BizException("-1","台账生成中，请勿重复生成。");
            }

            entity.setStatus(Constant.StatisticsStatus.CREAT_EXPORT_ING);
            entity.setFilename("");
            updateIgnoreNull(entity);
            statisId = entity.getId();
        }
        return statisId;
    }


    public int delete(StatisticsExportEntity statisticsExport){
        return statisticsExportWriteDao.delete(statisticsExport);
    }
}
